﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace PostProcGUI
{
    public class StyleConverter : IValueConverter
    {
        #region Implementation of IValueConverter

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            if (targetType != typeof(Style))
            {
                throw new InvalidOperationException("The target must be a Style");
            }

            var styleProperty = parameter as string;
            if (value == null || styleProperty == null)
            {
                return null;
            }

            var styleValue = value.GetType()
                .GetProperty(styleProperty)
                .GetValue(value, null)
                .ToString();

            return Application.Current.TryFindResource(styleValue);

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
